
#include <QtGui>

#include "glwidget.h"
#include "qMyWindow.h"

QMyWindow::QMyWindow(QWidget *parent)
: QMainWindow(parent)
{
	m_ui.setupUi(this);
	this->resize(600,600);

	connect( m_ui.actionExit, SIGNAL(triggered()), SLOT(quitApp()) );

	// background color
	connect( m_ui.switchBackgroundColorPushButton, SIGNAL(clicked(bool)), m_ui.glwidget, SLOT(slotSwitchBackgroundColor()) );
	// Show Group
	connect( m_ui.showTetrahedronRadioButton, SIGNAL(toggled(bool)), m_ui.glwidget, SLOT(slotShowTetrahedron(bool)) );
	connect( m_ui.showTetraFlakeRadioButton, SIGNAL(toggled(bool)), m_ui.glwidget, SLOT(slotShowTetraFlake(bool)) );
	connect( m_ui.showWholeSceneRadioButton  , SIGNAL(toggled(bool)), m_ui.glwidget, SLOT(slotShowSzene(bool)) );

	connect( m_ui.recursionDepthSpinBox, SIGNAL(valueChanged(int)), m_ui.glwidget, SLOT(setFlakeRecursionDepth(int)) );

	connect( m_ui.tetraHeightHorizontalSlider, SIGNAL(valueChanged(int)), this, SLOT(slotConvertFlakeHeightSliderValue(int)) );
	connect( this, SIGNAL(signalFlakeHeightSliderValueFloat(float)), m_ui.glwidget, SLOT(setFlakeTetraHeight(float)) );

	connect( m_ui.tetraHeightAttenuationHorizontalSlider, SIGNAL(valueChanged(int)), this, SLOT(slotConvertFlakeHeightAttenuationSliderValue(int)) );
	connect( this, SIGNAL(signalFlakeHeightAttenuationSliderValueFloat(float)), m_ui.glwidget, SLOT(setFlakeTetraHeightAttenuation(float)) );

	connect( m_ui.tetraChildSizeHorizontalSlider, SIGNAL(valueChanged(int)), this, SLOT(slotConvertFlakeChildSizeSliderValue(int)) );
	connect( this, SIGNAL(signalFlakeChildSizeSliderValueFloat(float)), m_ui.glwidget, SLOT(setFlakeTetraChildSize(float)) );

	initWidgets();
}

void QMyWindow::initWidgets()
{
	switch ( m_ui.glwidget->getViewMode() )
	{
	case GLWidget::TETRAHEDRON:
		m_ui.showTetrahedronRadioButton->setChecked( true );
		break;
	case GLWidget::TETRA_FLAKE:
		m_ui.showTetraFlakeRadioButton->setChecked( true );
	case GLWidget::SZENE:
		m_ui.showWholeSceneRadioButton->setChecked( true );
		break;
	default: break;
	}

	m_ui.recursionDepthSpinBox->setValue( m_ui.glwidget->getFlakeRecursionDepth() );

	setSliderValue( m_ui.tetraHeightHorizontalSlider, m_ui.glwidget->getFlakeTetraHeight() );
	setSliderValue( m_ui.tetraHeightAttenuationHorizontalSlider, m_ui.glwidget->getFlakeTetraHeightAttenuation() );
	setSliderValue( m_ui.tetraChildSizeHorizontalSlider, m_ui.glwidget->getFlakeTetraChildSize() );
}


// ====================== SLOTS =====================

void QMyWindow::quitApp() 
{ 
	emit signalQuitApp(); 
}

void QMyWindow::slotConvertFlakeHeightSliderValue(int)
{
	emit signalFlakeHeightSliderValueFloat( convertSlider2Float(*m_ui.tetraHeightHorizontalSlider) );
}

void QMyWindow::slotConvertFlakeHeightAttenuationSliderValue(int)
{
	emit signalFlakeHeightAttenuationSliderValueFloat( convertSlider2Float(*m_ui.tetraHeightAttenuationHorizontalSlider) );
}

void QMyWindow::slotConvertFlakeChildSizeSliderValue(int)
{
	emit signalFlakeChildSizeSliderValueFloat( convertSlider2Float(*m_ui.tetraChildSizeHorizontalSlider) );
}


// =================== PRIVATE MEMBER FUNCTIONS =========================

float QMyWindow::convertSlider2Float(const QSlider &qSlider)
{
	int smin = qSlider.minimum();
	int smax = qSlider.maximum();
	// normalized float value in [0,1]
	return (qSlider.value()-smin) / static_cast<float>(smax-smin);
}

void QMyWindow::setSliderValue(QSlider *qSlider, float v)
{
	int smin = qSlider->minimum();
	int smax = qSlider->maximum();
	qSlider->setValue( static_cast<int>( smin + v*(smax-smin) ) );
}